<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp"%>
<html>
<head>
	<title>${data.param_id==null?'添加':'修改'}SMSP参数</title>
	<style type="text/css">
		.inputNum {
			width: 60px;
			height: 20px;
		}
        .input__inner{
            min-height: 45px !important;
        }
        .el-input.el-input--large{
            width:400px;
            min-height: 45px !important;
        }
        .el-select__input.is-large{
            display: none;
        }
	</style>
    <!-- 引入element-ui样式 -->
    <link rel="stylesheet" href="${ctx}/js/element-ui/element-ui.css" />
    <!-- 引入element-ui组件库 -->
    <script src="${ctx}/js/element-ui/vue.min.js"></script>
    <script src="${ctx}/js/element-ui/element-ui.js"></script>
</head>
<body menuId="501">
<div class="container-fluid" menuI="1">
    <hr>
    <div class="row-fluid">
      <div class="span12">
        <div class="widget-box">
          <div class="widget-title"> <span class="icon"> <i class="fa fa-list"></i> </span>
            <h5></h5>
          </div>
          <div class="widget-content nopadding">
            <form class="form-horizontal" method="post" name="basic_validate" id="mainForm" novalidate="novalidate">
              <div class="control-group">
                <label class="control-label">参数名称</label>
                <div class="controls">
                	<s:if test="data.param_id == null">
                  		 <u:select id="param_key" value="${data.param_key}" dictionaryType="param_key"  excludeValue=""/>
                	</s:if>
                	<s:if test="data.param_id != null">
                		<input style="border-style:none; background: none;width: 300px" type="text" name="param_key" readonly="readonly" value="<u:ucparams key="${data.param_key}"  type="param_key"/>" maxlength="50"/>
                	</s:if>
                </div>
              </div>
              <div class="control-group">
                <label class="control-label">参数值</label>
			  <s:if test="data.param_key == 'LOG_LEVEL'">
				  <div class="controls">
					  <input type="text" id="logLevelVal" name="param_value" value="${data.param_value}" class="loglevelValidator" maxlength="20"/>
					  <span for="param_value"></span>
				  </div>
			  </s:if>

			  <!-- 查余额最小时间间隔 -->
			  <s:elseif test="data.param_key == 'GETBALANCE_INTERVAL'">
				  <div class="controls">
					  <input type="text" name="param_value" value="${data.param_value}" class="getBalanceValidator"/>
					  <span for="param_value"></span>
				  </div>
			  </s:elseif>


				  <!-- -每秒查询状态报告次数-->
			  <s:elseif test="data.param_key == 'QUERYREPORT_INTERVAL'">
				  <div class="controls">
					  <input type="text" name="param_value" value="${data.param_value}" class="queryReportValidator"/>
					  <span for="param_value"></span>
				  </div>
			  </s:elseif>

				  <!-- -全局模板数量-->
			  <s:elseif test="data.param_key == 'GLOBAL_TEMPLATE_NUMBER'">
				  <div class="controls">
					  <input type="text" name="param_value" value="${data.param_value}" class="positiveIntegerValidator"/>
					  <span for="param_value"></span>
				  </div>
			  </s:elseif>

				<!-- redis的pipeline配置  pipeline开关(0:关,1:开）;
										 pipeline最大缓存数
					-->
			  <s:elseif test="data.param_key == 'REDIS_PIPELINE_CFG'">
				  <s:set name="param_value" value='data.param_value.split("\\\\;")'/>
				  <div class="controls">
					  开关
					  <select name="redis_pipeline_cfg_0" style="width: 80px;">
						  <s:if test="#param_value[0] == 0">
							  <option value="0" selected>关闭</option>
							  <option value="1">开启</option>
						  </s:if>
						  <s:else>
							  <option value="0">关闭</option>
							  <option value="1" selected>开启</option>
						  </s:else>

					  </select>
					  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					  最大缓存数<input class="inputNum" type="text" name="redis_pipeline_cfg_1" value="<s:property value="#param_value[1]"/>" maxlength="20"/>
				  </div>
				  <div class="controls" style="display: none" id="div_audit_valid">
					  <span style="color: red; " id="audit_valid"></span>
				  </div>
				  <input id="REDIS_PIPELINE_CFG" name="param_value" type="hidden" value=""/>
			  </s:elseif>
                  <s:elseif test="data.param_key == 'KEYWORD_CONVERT_REGULAR'">
                  <s:set name="param_value" value='data.param_value.split("\\\\;")'/>
              </div>
                <div class="control-group">
                    <label class="control-label"><span style="color: red">*</span>参数1：</label>
                    <div class="controls">
                        <select name="keyword_convert_regular_0" style="width: 110px;">
                            <s:if test="#param_value[0] == 1">
                                <option value="1" selected>系统关键字</option>
                                <option value="2">审核关键字</option>
                            </s:if>
                            <s:else>
                                <option value="1" >系统关键字</option>
                                <option value="2" selected>审核关键字</option>
                            </s:else>

                        </select>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    </div>
                    <label class="control-label"><span style="color: red">*</span>参数2：</label>
                    <div  id="tranlateRule" class="channelid controls">
                        <el-select v-model="value1" name="keyword_convert_regular_1" multiple filterable  size="large" placeholder="请选择参数">
                            <el-option
                                    v-for="item in options1"
                                    :label="item.ruleName"
                                    :value="item.ruleValue">
                            </el-option>
                        </el-select>
                    </div>
                    <input id="KEYWORD_CONVERT_REGULAR" name="param_value" type="hidden" value=""/>
                    <input id="keyword_convert_regular_1_value"  type="hidden" value=""/>

                </div>

                </s:elseif>
			  <!-- DB消息重入队列配置 -->
			  <s:elseif test="data.param_key == 'DB_REQUEUE_CFG'">
				  <s:set name="param_value" value='data.param_value.split("\\\\;")'/>
				  <s:set name="rebackNum" value='#param_value[0].split("\\\\/")'/>
				  <s:set name="rebackTime" value='#param_value[1].split("\\\\/")'/>
				  <div class="controls">
					  <input class="inputNum" type="text" name="DB_REQUEUE_CFG_0" value="<s:property value="#rebackNum"/>" class="positiveIntegerValidator"/>
					  次&nbsp;&nbsp;&nbsp;&nbsp;
					  <input class="inputNum" type="text" name="DB_REQUEUE_CFG_1" value="<s:property value="#rebackTime"/>" class="positiveIntegerValidator"/>
					  秒
					  <input id="DB_REQUEUE_CFG" name="param_value" style="display:none;" value=""/>
				  </div>
			  </s:elseif>
			  <!-- 系统监控开关 -->
			  <s:elseif test="data.param_key == 'MONITOR_SWITCH'">
				  <s:set name="param_value" value='data.param_value.split("\\\\;")'/>
				  <s:set name="monitor_switch_cfg_0" value='#param_value[0]'/>
				  <s:set name="monitor_switch_cfg_1" value='#param_value[1]'/>
				  <s:set name="monitor_switch_cfg_2" value='#param_value[2]'/>
				  <s:set name="monitor_switch_cfg_3" value='#param_value[3].split("\\\\:")'/>
				  <s:set name="monitor_switch_cfg_3_1" value='#monitor_switch_cfg_3[0]'/>
				  <s:set name="monitor_switch_cfg_3_2" value='#monitor_switch_cfg_3[1]'/>
				  <s:set name="monitor_switch_cfg_4" value='#param_value[4]'/>
				  <div>
					  <div class="control-group">
						  <div class="controls">
							  <select name="monitor_switch_cfg_0" style="width: 100px;">
								  <s:if test ="#monitor_switch_cfg_0 ==0 ">
									  <option value="0" selected>关闭</option>
									  <option value="1">开启</option>
								  </s:if>
								  <s:else>
									  <option value="0">关闭</option>
									  <option value="1" selected>开启</option>
								  </s:else>
							  </select>
							  <span class="red">*</span>系统开关
						  </div>
					  </div>
					  <div class="control-group">
						  <label class="control-label"></label>
						  <div class="controls">
							  <select name="monitor_switch_cfg_1" style="width: 100px;">
								  <s:if test="#monitor_switch_cfg_1 == 0">
									  <option value="0" selected>关闭</option>
									  <option value="1">开启</option>
								  </s:if>
								  <s:else>
									  <option value="0">关闭</option>
									  <option value="1" selected>开启</option>
								  </s:else>
							  </select>
							  <span class="red">*</span>是否使用MQ
						  </div>
					  </div>
					  <div class="control-group">
						  <div class="controls">
							  <input class="inputNum" type="text" name="monitor_switch_cfg_2" style="width: 86px;" value="<s:property value="#monitor_switch_cfg_2"/>" class="positiveIntegerValidator"/>
							  <span class="red">*</span>线程数量
						  </div>
					  </div>
					  <div class="control-group">
						  <div class="controls">
							  <input  type="text" name="monitor_switch_cfg_3_1"  value="<s:property value="#monitor_switch_cfg_3_1"/>" />InfluxDB服务器地址<span class="red">*</span>
							  <input  type="text" name="monitor_switch_cfg_3_2"  value="<s:property value="#monitor_switch_cfg_3_2"/>" />端口<span class="red">*</span>
						  </div>
					  </div>
					  <div class="control-group">
						  <div class="controls">
							  <input  type="text" name="monitor_switch_cfg_4" value="<s:property value="#monitor_switch_cfg_4"/>" />
							  认证账号
						  </div>
					  </div>
					  <input id="MONITOR_SWITCH" name="param_value" style="display:none;" value=""/>
				  </div>
			  </s:elseif>

			  <s:else>
				  <div class="controls">
					  <input type="text" id="other" name="param_value" value="${data.param_value}"  maxlength="100"/>
					  <span for="param_value"></span>
				  </div>
			  </s:else>
              <div class="control-group">
                <label class="control-label">描述</label>
                <div class="controls">
                  <input type="text"  name="description" value="${data.description}"  maxlength="150" style="width: 490px"/>
                </div>
              </div>
			  <div class="control-group">
                <label class="control-label">&nbsp;</label>
                <div class="controls">
                  <span id="msg" class="error" style="display:none;"></span>
                </div>
              </div>
              <div class="form-actions">
	              <input type="hidden" name="param_id" value="${data.param_id}" />
	              <input type="button" value="${data.param_id==null?'添 加':'修 改'}" class="btn btn-success" onclick="save(this)"/>
	              <input type="button" value="取 消" class="btn btn-error" onclick="back()"/>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>

<script type="text/javascript">
var validate;
var overRate_validate = true; //超频参数校验
var cw_validate = true; //通道成功率预警
var error_validate=true; //默认状态报告校验
var pkey="${data.param_key}";
var pvalue="${data.param_value}";
$(function(){
	$.validator.defaults.ignore = "";
	//表单验证规则
	validate = $("#mainForm").validate({
		errorPlacement: function(error, element) {
			$(element).closest( ".controls" ).find( "span[for='" + element.attr( "name" ) + "']" ).append( error );
		},
		errorElement: "span",
		rules: {
			key: "required",
			value: "required",
			param_value: { 
//				required : true,
                requiredValidator : true
			}
		},
		messages: {
			key: "请输入参数键",
			value: "请输入参数值"
		}
	});



	jQuery.validator.addMethod("requiredValidator", function(value, element) {
		if( value.trim() == '' ){
		    if(element.getAttribute("class")=='keywordRegularValidator'){
		        return true;
			}
			return false;
		}
		return true;
	}, "必须填写");

	// 日志级别参数值校验
	jQuery.validator.addMethod("loglevelValidator", function(value, element) {
		if( value.trim() == '' ){
			return false;
		}
	    var reg = /^[0-8]{1}$/;
	    return this.optional(element) || (reg.test(value));
	}, "请输入0到8中的一个整数");


	
	// 用户查费接口调用频率时间范围校验
	jQuery.validator.addMethod("getBalanceValidator", function(value, element) {
		if( value.trim() == '' ){
			return false;
		}
	    if( Number(value) < 1 || Number(value) > 300 ){
	    	return false;
	    }
	    return true;
	}, "请输入1到300中的一个整数");

	
	jQuery.validator.addMethod("queryReportValidator", function(value, element) {
		if( value.trim() == '' ){
			return false;
		}
		if( !/^\d+$/.test(value)){
	    	return false;
	    }
		if( Number(value) < 1 || Number(value) > 100 ){
	    	return false;
	    }
		return true;
	}, "请输入1到100中的一个整数");

	//正整数校验
	jQuery.validator.addMethod("positiveIntegerValidator", function(value, element) {
		if( value.trim() == '' ){
			return false;
		}
		if( !/^[0-9]*$/.test(value)){
	    	return false;
	    }
        if( Number(value) < 0 ){
            return false;
        }
		return true;
	}, "请输入正整数");


});

function save(btn){
 	$("#msg").hide();
	var pv  = $("input[name='param_value']");
    if(pv.attr("id") == "REDIS_PIPELINE_CFG") {
        var redis_pipeline_cfg_0 = $("select[name='redis_pipeline_cfg_0']").val();
        var redis_pipeline_cfg_1 = $("input[name='redis_pipeline_cfg_1']").val();
        var div_audit_valid = $("#div_audit_valid");
        var audit_valid = $("#audit_valid");

        if(redis_pipeline_cfg_0 == "" || redis_pipeline_cfg_1 == ""){
            audit_valid.text("参数不能为空");
            div_audit_valid.css("display","block");
            return;
        }else if(!/^\d+$/.test(redis_pipeline_cfg_0) || !/^\d+$/.test(redis_pipeline_cfg_1)) {
            audit_valid.text("设置参数需填写非负整数");
            div_audit_valid.css("display","block");
            return;
        }else if(redis_pipeline_cfg_0!=1 && redis_pipeline_cfg_0!=0){
            audit_valid.text("开关值有误");
            div_audit_valid.css("display","block");
            return;
        }else if(redis_pipeline_cfg_1<0 || redis_pipeline_cfg_1>10000){
            audit_valid.text("pipeline最大缓存数范围应为[0,10000]");
            div_audit_valid.css("display","block");
            return;
        }else{
            audit_valid.text("");
            div_audit_valid.css("display","none");
        }
        var value = redis_pipeline_cfg_0  + ";" + redis_pipeline_cfg_1;
        pv.val(value);
    } else if (pv.attr("id") == "DB_REQUEUE_CFG") {
        var rebackNum = $("input[name='DB_REQUEUE_CFG_0']").val().trim();
        var rebackTime = $("input[name='DB_REQUEUE_CFG_1']").val().trim();
        if(rebackNum =="" || rebackTime== ""){
            layer.msg("参数不能为空",{icon:2});
            return;
		}
        if(!/^\d+$/.test(rebackNum) || !/^\d+$/.test(rebackTime)) {
            layer.msg("参数需填写非负整数",{icon:2});
            return;
        }

        var rebackNum_Int = parseInt(rebackNum);
        var rebackTime_Int = parseInt(rebackTime);
		if (!(rebackNum_Int >= 0 && rebackNum_Int <= 1000)) {
			layer.msg("次数范围必须在0-1000之间",{icon:2});
			return;
		}
		if (!(rebackTime_Int >= 1 && rebackTime_Int <= 86400)) {
			layer.msg("时间范围必须在1-86400之间",{icon:2});
			return;
		}

        var value = rebackNum_Int + ";" + rebackTime_Int;
        pv.val(value);
    }else if(pv.attr("id") == "MONITOR_SWITCH"){
        var systemSwitch = $("select[name='monitor_switch_cfg_0']").val();
        var useMQ = $("select[name='monitor_switch_cfg_1']").val();
        var threadNum = $("input[name='monitor_switch_cfg_2']").val().trim();
        var ipAddress = $("input[name='monitor_switch_cfg_3_1']").val().trim();
        var port = $("input[name='monitor_switch_cfg_3_2']").val().trim();
        var account = $("input[name='monitor_switch_cfg_4']").val().trim();

		if(systemSwitch=="" || useMQ=="" ||threadNum=="" || ipAddress=="" || port==""){
            layer.msg("必填参数不能为空",{icon:2});
            return;
		}
        if(!/^\d+$/.test(threadNum)) {
            layer.msg("线程数量需填写非负整数",{icon:2});
            return;
        }else{
		    if(!(threadNum>=1 && threadNum<=16)){
                layer.msg("线程数量在1-16之间",{icon:2});
                return;
			}
		}

		var ipRex = /^(((1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[1-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)))$/;
		if(!ipRex.test(ipAddress)){
            layer.msg("InfluxDB服务器地址格式不正确",{icon:2});
            return;
		}

        if(!/^\d+$/.test(port)) {
            layer.msg("端口需填写非负整数",{icon:2});
            return;
        }

        if(account !="" && account !=null){
			account = ";"+account;
		}else{
            account = "";
		}
		var value = systemSwitch+";"+useMQ+";"+threadNum+";"+ipAddress+":"+port+account;
        pv.val(value);
    }else if(pv.attr("id") == "KEYWORD_CONVERT_REGULAR"){
        var div_audit_valid = $("#div_audit_valid");
        var audit_valid = $("#audit_valid");
        var regular_0 =  $("select[name='keyword_convert_regular_0']").val();
        var regular_1 = $("#keyword_convert_regular_1_value").val();
        if(regular_0 == "" || regular_0 == null){
            alert("参数1不能为空");
            return;
        }
        if(regular_1 == "" || regular_1 == null){
            alert("参数2不能为空");
            return;
        }
        var values = regular_1.split(",");
        var ruleList  = 0;
        for(var i = 0 ; i<values.length;i++){
            var intValue = parseInt(values[i]);
            ruleList =parseInt(ruleList) +intValue ;
        }
        ruleList =regular_0 +";" +ruleList;
        pv.val(ruleList);
    }else {
		if(!validate.form()){
			return;
		}
    }
	
	var options = {
		beforeSubmit : function() {
			$(btn).attr("disabled", true);
		},
		success : function(data) {
			$(btn).attr("disabled", false);
			
			if(data.result==null){
				$("#msg").text("服务器错误，请联系管理员").show();
				return;
			}
			
			// $("#msg").text(data.msg).show();
            layer.msg(data.msg, {icon: 1,time: 1500},function(){
                back();
            });

		},
		url : "${ctx}/smspParamConf/save",
		type : "post",
		timeout:30000
	};
	$("#mainForm").ajaxSubmit(options);
};


vm = new Vue({
    el : "#tranlateRule",
    mounted: function(){
        var dataArr = [];
        var split = pvalue.split(";");
        //转化为二进制
        var ruleValue = parseInt(split[1]).toString(2);
        for(var i = 0 ; i<ruleValue.length;i++){
            var value = ruleValue.charAt(i);
            var index = ruleValue.length-1-i;
            if(value == "1"){
                dataArr.push(Math.pow(2,index)+"");
            }
        }

        data = [{"ruleName": "繁体转简体", "ruleValue": "1"},
            {"ruleName": "大写转小写", "ruleValue": "2"},
            {"ruleName": "全角转半角", "ruleValue": "4"},
            {"ruleName": "去空格", "ruleValue": "8"},
            {"ruleName": "中文符号转英文符号", "ruleValue": "16"},
            {"ruleName": "删除其他字符", "ruleValue": "32"}];
        var that = this;
        that.options1 = data;
        that.value1 = dataArr;
    },
    data : {
        options1 : [],
        value1: ''
    },
    watch : {
        value1 : function(value){
            this.$nextTick(function(){
                $("#keyword_convert_regular_1_value").val(value);
            });
        }
    }
});


</script>
</body>
</html>